#include <dos.h>
#include <stdio.h>
#include "ae.h"

/********* swap buff ***********/

#define MAXBUF 16384
#define CHECKMAX MAXBUF/2
#define MAXCLUSTER 61700

unsigned short wbuf0[checkmax];
unsigned short wbuf1[checkmax];

unsigned char wbuf0_full, wbuf1_full;
unsigned long wbuf_count;
unsigned char wmode;

/******** interrupt *********/
void far interrupt t1_isr (void);

/************* for File write *************/
#include "mm16.h"
#include "fileio.h"
#include filegeo.h"

#define FS_NO_DMA

struct ff_geo *ff_geo;
struct fs_descrip *file;
char filename[FNLEN+2] = "test.bin";
char fstatus;

void initFileSys(void);
void RemoveFile(void);
void OpenNewFile(void);

void PAUSE (void);

/*********** AD Convector **********/

#define ADB 0x120
#define ADA 0x124
#define CV 0x140

void main (void)

{
	char current_key;
   int tm,ta,tb;
   unsigned long cluster_count;
   char quit;
   char out[10];


   wbuf0_full = 0 ; wbuf1_full = 0; wmode = 0;

   ae_init();
   outport (0xfff2, 0);

	initFileSys();
   RemoveFile();
   OpenNewFile();


   pio_init (9,2); pio_wr (9,1);
   pio_init (19,2); pio_wr(19,1);
   pio_init (18,2); pio_wr(18,1);

   pio_init (12,2); pio_wr(12,0);

   pio_init(1,0);
   tm=0xE001;ta=(unsigned int) 250; tb=(unsigned int) 250;

   outport (0xffa2,0x7fbc);
   outport (0xffa4,0x0075);

   t1_init (tm,ta,tb,t1_isr);


   cluster_count = MAXCLUSTER-1;
   quit = 0;
   while (quit==0)
   {
   	switch (wmode)
      	{
         case 0 : if (wbuf1_full ==1)
         				{	//save 1 to file
            				pio_wr(18,0);   	//J2-15
              				fstatus = fs_fputcluster((unsigned char*) (&wbuf1),file);

               			wbuf1_full = 0;
               			pio_wr(18,1);

               			cluster_count --;
                     }
                     break;
         case 1 : if (wbuf0_full==1)
         				{ //save 0 to file
                     	pio_wr(19,0);     //J2-16
                        fstatus = fs-fputcluster((unsigned char*) (&wbuf1), file);

                        wbuf0_full = 0;
                        pio_wr(19,1);

                        cluster_count--;
                     }
        }
      if (cluster_count == 0)
      { // error (full) = 1
         quit = 1;
      }
   }

   t1_init(0x0000,0,0,t1_isr); // turn off timer
   fs_fclose(file);
}

void interrupt t1_isr (void)
{
	inport(CV); // start conversion for AA1 and AB1

   switch
   	{
      	case 0 : if (wbuf0_full==0)
         			{
                  	wbuf0[wbuf_count]=(inport(ADA));
                     if (wbuf_count < CHECKMAX-1)
                     	{ wbuf_count++; }
                     else
                     	{ wbuf_count = 0; wmode = 1; wbuf0_full = 1;}
                     break;
                  }
                  else
                  {
                  	t1_init(0x0000,0,0,t1_isr); //turn off timer
                     // error buffer 0 full
                  }
                  break;

         case 1 : if (wbuf1_full==0)
         			{
                  	wbuf1[wbuf_count]=inport(ADA);
                     if (wbuf_count < CHECKMAX-1 )
                     	{ wbuf_count++; }
                     else
                     	{ wbuf_count = 0; wmode = 0; wbuf1_full = 1; }
                     break;
                  }
                  else
                  { t1_init(0x0000,0,0,t1_isr);
                  	// error buffer 1 full
                  }
                  break;
         }
   outport(0xff22,0x0008);  // issue EOI for the interrupt.
}

void initFileSys (void)
{
	char cfg[80];

#ifdef FS_NO_DMA
   fs_setFlags(FS_NO_DMA0);
#endif

#ifdef TERN_16_BIT
	outport(0xfff2, 0); // AUXCON, MCS, but 16-bit
#endif

#ifdef TERN_RE
	//fs_setCFAddress (PCM3_RE);
#endif

	if (fs_initPCFlash()==gDISKBUSY)
   {	//error not CF card
   }
}

void OpenNewFile (void)
{
	//use global variable
   char str[30];
   unsigned long totalclusters;
   unsigned long i,j;
   int current_key;

   totalclusters = MAXCLUSTER;
   //LED shine-> allocate file.
}

void RemoveFile(void){

     int retval;

     if ((file = fs_fopen(filename, O_RDONLY))==NULL)
     { // LED shine -> fail to delete
     	return;
     }

     if (file->ff_status!=fOK)
     {
     	fs_fclose(file);
       // LED shine -> fail to delete
      return;
     }

     //LED -> write file
     retval = fs_fremove(file);

     if (retval != fOK)
     {
      // LED shine -> fail to delete
     }
}





